<!DOCTYPE html>
<%@page import="java.text.SimpleDateFormat"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理者</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap-theme.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/public.css">
<script type="text/javascript" src="${contextPath}/js/jquery.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/common.js"></script>
</head>
<body>
	
	<div class="container">
		<div class="panel panel-default">
			<div class="panel-heading">用户列表<span id="taskMarkTimeMonthTotle" class="pull-right text-success">管理员：${sessionScope.username} | <a href="${contextPath}/quit">退出</a></span>
			</div>
			<div class="panel-body">
				<form class="form-inline" role="form">
					<div class="form-group">
						<div class="input-group">
							<div class="input-group-addon"> 用户名：</div>
							<input class="form-control" onkeydown="if(event.keyCode==13){return false;}" id="userNameCondition" type="text" placeholder="查询用户">
						</div>
					</div>
					<button type="button" id="searchBtn" class="btn btn-primary">查询</button>
					<button type="button" id="addUserBtn" class="btn btn-primary">添加用户</button>
				</form>
			</div>
			<table class="table table-striped table-bordered">
				<thead>
					<tr>
						<th width='5%'>序号</th>
						<th width='15%'>用户名</th>
						<th width='20%'>姓名</th>
						<th width='10%'>创建时间</th>
						<th width='20%'>用户类型</th>
						<th width='10%'>用户状态</th>
						<th width='10%'>修改密码</th>
						<th width='10%'>操作</th>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
			<ul class="pagination"></ul>
		</div>
		<div class="modal fade" id="changeStatus">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
						<h4 class="modal-title">修改状态</h4>
					</div>
					<div class="modal-body">
						<span id="updateStatus"></span>
						<form class="form-inline" role="form">
						<div class="form-group">
							<label class="radio-inline">
								<input type="radio" class="changeust" name="inlineRadioOptions" id="" value="1" autocomplete="off">可用
							</label>
					   </div>
					   <div class="form-group">
							<label class="radio-inline">
								<input type="radio" class="changeust" name="inlineRadioOptions" id="" value="0" autocomplete="off">禁用
							</label>
					   </div>
					   </form>
					</div>
					<div class="modal-footer">
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
						<button type="button" id="changeUserStatusBtn" class="btn btn-primary">修改</button>
					</div>
				</div><!-- /.modal-content -->
			</div><!-- /.modal-dialog -->
		</div><!-- /.modal -->
	</div>
	<!-- 删除模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog">
      <div class="modal-content">
         <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times;</button>
            <h4 class="modal-title">重要提示</h4>
         </div>
         <div class="modal-body">确定要删除该用户？</div>
         <div class="modal-footer">
            <button type="button" class="btn btn-default" 
               data-dismiss="modal">关闭
            </button>
            <button type="button" class="btn btn-primary" id="deleteButton">删除</button>
         </div>
      </div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>

	<!-- 修改密码模态框（Modal） -->
<div class="modal fade" id="changePwdModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog">
      <div class="modal-content">
         <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> &times;</button>
            <h4 class="modal-title">修改密码</h4>
         </div>
         <div class="modal-body">
				<form action="${contextPath}/security/addUser" method="post" id="addUser" name="addUser" role="form" class="form-horizontal">
					<input type="hidden" value="" id="changeId">
					<div class="form-group">
						<label for="password" class="col-sm-2 control-label">登录密码：</label>
						<div class="col-sm-10">
							<input type="password" class="form-control" name="password" id="password" placeholder="请输入密码" autocomplete="off"> <span class="help-block"></span>
						</div>
						<label for="repassword" class="col-sm-2 control-label">确认密码：</label>
						<div class="col-sm-10">
							<input type="password" class="form-control" name="repassword" id="repassword" placeholder="请输入密码" autocomplete="off"> <span class="help-block"></span>
						</div>
					</div>
				</form>         
         
         </div>
         <div class="modal-footer">
            <button type="button" class="btn btn-default" 
               data-dismiss="modal">关闭
            </button>
            <button type="button" class="btn btn-primary" onclick="changePwdFun();">修改</button>
         </div>
      </div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
	<script type="text/javascript">
		var deleteUserId=null;
		var utDropdownOpen = false;
		var nowUserType = 4;
		var nowPage = 0;
		var userNameCondition = "";
		var month = 0;
		var taskUpload = 2;
		var year = 0;
		var userId = 0;
		var pageTotal = 0;
		$(document).ready(function(){
			chooseUserType(1);
			/*--------------------------------------点击查询按钮-------------------------------------------------------*/
			$("#searchBtn").click(function(){
				chooseUserType(1);
			});
			$("#addUserBtn").click(function(){
				location.href="${contextPath}/security/addUser";
			});
			$("#quit").click(function(){
				location.href="${contextPath}/quit";
			});
			$("#deleteButton").click(function(){
				if(deleteUserId!=null){
					$.ajax({
						type:'POST',
						data:{"userId":deleteUserId},
						url:'${contextPath}/security/deleteUser',
						dataType:'json',
						success:function(data){
							chooseUserType(1);
							$("#myModal").modal('hide');
							alert(data.message);
						}
					});
				}
			});
			$("#myModal").on('hide.bs.modal', function () {
      			deleteUserId=null;
      		});
			/*--------------------------------------点击更改用户状态-------------------------------------------------------*/
			$("#changeUserStatusBtn").click(function(){
				var ust = $(".changeust:checked").val();
				var ustv = "不可用";
				if(ust == 1)ustv = "可用";
				$.ajax({
					type:'POST',
					data:{"userId":userId,"userStatus":ust},
					url:'${contextPath}/security/userStatus',
					dataType:'json',
					success:function(data){
						alert(data.message);
						//$("#updateStatus").addClass("text-success").text(data.message);
						$("#changeStatus").modal('hide');
						chooseUserType(1);
					}
				});
			});
			/*--------------------------------------点击删除用户-------------------------------------------------------*/

			deleteUser = function(userId,username){
				if(username=='admin'){
					alert("超级管理员不可以被删除！");
					return;
				}
				deleteUserId=userId;
				$("#myModal").modal('show');
			};
			updatePwd = function(userId,username){
				$("#changeId").val(userId);
				$("#changePwdModal").modal('show');
			};
			$("#changeStatus").on('hidden.bs.modal', function (e) {
				$("#updateStatus").empty();
				$(".radio-inline input").attr("checked"," ");
			});
			
		});
		/*--------------------------------------更改用户状态-------------------------------------------------------*/
		changeUserStatus = function(uId,userStatus,username){
			userId = uId;
			if(userStatus == 1){
				$(".radio-inline input[value=1]").attr("checked","checked");
			}else{
				$(".radio-inline input[value=0]").attr("checked","checked");
			}
			$("#changeStatus").modal('show');
			
		};
		/*--------------------------------------加载页面-------------------------------------------------------*/
		chooseUserType = function(pageNum){
		var name = $("#userNameCondition").val();
			$.ajax({
				type:'POST',
				data:{"name":name,"page":pageNum},
				url:'${contextPath}/security/manager',
				dataType:'json',
				success:function(data){
					$("tbody").empty();
					if(data.list == ""){
						$("tbody").empty();
						$("tbody").append("<tr class='text-danger'><td colspan='6'>无内容</td></tr>");
					}else{
						pageTotal = data.totlePage;
						$.each(data.list,function(i,item){
							var status = "不可用";
							if(item.userStatus == "1")status = "可用";
							$("tbody").append(
								"<tr>"+
									"<td>"+(i+1)+"</td>"+
									"<td>"+item.username+"</td>"+
									"<td>"+item.realname+"</td>"+
									"<td>"+item.createTime+"</td>"+
									"<td>"+item.usertype+"</td>"+
									"<td><a href='#' onClick='changeUserStatus("+item.userId+","+item.userStatus+",\""+item.username+"\")'>"+status+"</a></td>"+
									"<td><a href='#' onClick='updatePwd("+item.userId+",\""+item.username+"\")'>修改密码</a></td>"+
									"<td><a href='#' onClick='deleteUser("+item.userId+",\""+item.username+"\")'>删除</a></td>"+
								"</tr>"
							);
							var pageDom = $(".pagination");
							pageDom.empty();
							page.creatPageHTML(pageNum,pageTotal,pageDom,"chooseUserType");
							/*--------------------------------------跳转页-------------------------------------------------------*/
							$(".pageGoBtn").click(function(){
								var pageNum = 0;
								pageNum = $(".pageGoText").val();
								if(pageNum !=0&&0<pageNum&&pageNum<pageTotal+1){
									chooseUserType(pageNum);
								}
							});
						});
					}
				}
			});
		}
		/*--------------------------------------修改密码------------------------------------------------------*/
		changePwdFun = function(){
		var p1= $("#password").val();
		var p2= $("#repassword").val();
		if(p1!=p2){
			alert("错误，两次输入的密码不一致！");
			return;
		}
		var changeId = $("#changeId").val();
		$.ajax({
			type:'POST',
			data:{"password":p1,"userId":changeId},
			url:'${contextPath}/security/changePwd',
			dataType:'json',
			success:function(data){
				alert("密码修改成功！");
				$("#changePwdModal").modal('hide');
			}
		});
		}
	</script>
</body>
</html>
